package com.chinapost.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.chinapost.service.DeptServiceI;
import com.chinapost.service.WdsbServiceI;
import com.chinapost.vo.Json;
import com.chinapost.vo.SessionInfo;
import com.chinapost.vo.Wdsb;
import com.opensymphony.xwork2.ModelDriven;

@Action(value = "wdsbAction", results = { 
		@Result(name = "wdsbManager", location = "/jsp/wdsb/wdsbManager.jsp"),
		@Result(name = "wdsbAdd", location = "/jsp/wdsb/wdsbAdd.jsp"), 
		@Result(name = "wdsbImport", location = "/jsp/wdsb/wdsbImport.jsp"),
		@Result(name = "wdsbEdit", location = "/jsp/wdsb/wdsbEdit.jsp") 
		})
public class WdsbAction extends BaseAction implements ModelDriven<Wdsb>, ServletRequestAware {

	private Wdsb wdsb = new Wdsb();
	private WdsbServiceI wdsbService;
	private DeptServiceI deptService;
	private HttpServletRequest request;

	public DeptServiceI getDeptService() {
		return deptService;
	}

	@Autowired
	public void setDeptService(DeptServiceI deptService) {
		this.deptService = deptService;
	}

	public WdsbServiceI getWdsbService() {
		return wdsbService;
	}

	@Autowired
	public void setWdsbService(WdsbServiceI wdsbService) {
		this.wdsbService = wdsbService;
	}

	@Override
	public Wdsb getModel() {
		return wdsb;
	}

	public String wdsbManager() {
		return "wdsbManager";
	}

	public String wdsbEdit() {
		return "wdsbEdit";
	}

	public String wdsbAdd() {
		return "wdsbAdd";
	}

	public String wdsbImport() {
		return "wdsbImport";
	}


	public void add() {
		Json j = new Json();
		try {
			wdsbService.add(wdsb);
			j.setSuccess(true);
			j.setMsg("添加成功！");
		} catch (Exception e) {
			j.setMsg("添加失败！");
		}
		super.writeJson(j);
	}

	public void addWdsbs() {
		Json j = new Json();
		int count = 0;
		try {
			count = wdsbService.addWdsbs(wdsb);
			if (count > 0) {
				j.setSuccess(true);
				j.setMsg("上传成功，新增" + count + "条记录！");
			} else {
				j.setMsg("上传失败,新增0条记录！");
			}
		} catch (Exception e) {
			j.setMsg("上传失败,新增0条记录！");
		}
		super.writeJson(j);
	}

	public void edit() {
		Json j = new Json();
		try {
			wdsbService.edit(wdsb);
			j.setSuccess(true);
			j.setMsg("编辑成功!");
		} catch (Exception e) {
			j.setMsg("编辑失败！");
		}
		writeJson(j);
	}

	public void delete() {
		Json j = new Json();
		try {
			wdsbService.delete(wdsb);
			j.setSuccess(true);
			j.setMsg("删除成功！");
			j.setObj(wdsb.getCid());
		} catch (Exception e) {
			j.setMsg("删除失败！");
		}
		super.writeJson(j);
	}

	public void datagrid() {
		if (!StringUtils.isEmpty(wdsb.getDeptId())) {
			wdsb.setDeptIds(deptService.getChildIds(wdsb.getDeptId()));
		} else {
			SessionInfo s = (SessionInfo)request.getSession().getAttribute("sessionInfo");
			wdsb.setDeptIds(deptService.getChildIds(s.getDeptid()));
		}
		super.writeJsonDate(wdsbService.datagrid(wdsb));
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;

	}
}
